package com.service.Impl;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mapper.TopPickMapper;
import com.mapper.TopPickTicketMapper;
import com.pojo.dto.Message;
import com.pojo.entity.CTopPick;
import com.pojo.entity.CTopPickTicket;
import com.pojo.vo.SelectVo;
import com.pojo.vo.TopPickVo;
import com.service.SysUtilService;
import com.service.TopPickService;
import com.service.TopPickTicketService;
import com.util.PojoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service("TopPickService")
public class TopPickServiceImpl implements TopPickService {
    @Autowired
    TopPickMapper topPickMapper;
    @Autowired
    Snowflake snowflake;
    @Autowired
    TopPickTicketMapper topPickTicketMapper;
    @Autowired
    TopPickTicketService topPickTicketService;
    @Autowired
    SysUtilService sysUtilService;
    @Override
    public Message<List<TopPickVo>> list(TopPickVo topPickVo) {
        QueryWrapper<CTopPick> wrapper = new QueryWrapper<>();
        List<CTopPick> cTopPicks = topPickMapper.selectList(wrapper);
        List<TopPickVo> c_top_state = cTopPicks.stream().map(c -> {
            TopPickVo topPickVo1 = PojoUtil.copyObject(c, TopPickVo.class);
            SelectVo city_num = sysUtilService.selectone(topPickVo1.getState(), "c_top_state");
            topPickVo1.setStateName(city_num.getText());
            return topPickVo1;
        }).collect(Collectors.toList());
        return Message.successMessage((long) cTopPicks.size(), c_top_state);
    }

    @Override
    public Message<TopPickVo> get(TopPickVo topPickVo) {
        QueryWrapper<CTopPick> wrapper = new QueryWrapper<>();
        if (topPickVo.getId() != null) wrapper.eq("id", topPickVo.getId());
        if (topPickVo.getBatchNum() != null) wrapper.eq("batch_num", topPickVo.getBatchNum());
        CTopPick cTopPick = topPickMapper.selectOne(wrapper);
        return Message.successMessage(1L, PojoUtil.copyObject(cTopPick, TopPickVo.class));
    }

    @Override
    public Message<TopPickVo> edit(TopPickVo topPickVo) {
        CTopPick cTopPick = PojoUtil.copyObject(topPickVo, CTopPick.class);
        int i = topPickMapper.updateById(cTopPick);//根据ID更新book表
        if (i == 0) return Message.errorMessage("1001", "更新失败");
        else return Message.successMessage(1L, topPickVo);
    }

    @Override
    public Message<TopPickVo> add(TopPickVo topPickVo) {
        CTopPick cTopPick = PojoUtil.copyObject(topPickVo, CTopPick.class);
        cTopPick.setCreateTime(new Date());
        Calendar nowTime1 = Calendar.getInstance();
        nowTime1.setTime(cTopPick.getCreateTime());
        nowTime1.add(Calendar.HOUR, 10);//要增加什么，在这里写
        cTopPick.setPickTime(nowTime1.getTime());
        cTopPick.setBatchNum(snowflake.nextId());
        int i = topPickMapper.insert(cTopPick);
        if (i == 0) return Message.errorMessage("1001", "添加失败");
        else return Message.successMessage(1L, topPickVo);
    }

    @Override
    public Message<TopPickVo> delete(TopPickVo topPickVo) {
        int i = topPickMapper.deleteById(topPickVo.getId());
        if (i == 0) return Message.errorMessage("1001", "删除失败");
        else return Message.successMessage(1L, null);
    }

    //头筹状态修改
    @Override
    public Message<TopPickVo> state(TopPickVo topPickVo) {

        QueryWrapper<CTopPickTicket> wrapper = new QueryWrapper<>();
        wrapper.eq("batch_num", topPickVo.getBatchNum());
        Long count = topPickTicketService.count(topPickVo.getBatchNum());
        Message<TopPickVo> topPickVoMessage = this.get(topPickVo);

        if (topPickVoMessage.getData().getUserCount().longValue() > count.longValue()) {
            topPickVoMessage.getData().setState(3L);//参与人数小于限制人数设置为流筹
            return this.edit(topPickVoMessage.getData());
        } else {
            topPickVoMessage.getData().setState(2L);

            topPickTicketService.open(topPickVoMessage.getData());//如果开筹，显示开奖人信息。
            return this.edit(topPickVoMessage.getData());
        }


    }


}
